package interactiond

import (
	"time"

	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"github.com/pkg/errors"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	cinteractiond "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/interactiond"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/repo/mgorepo/baser"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

type interactionRepo struct {
	baser.BaseRepo
}

// 新增互动
func (r interactionRepo) AddInteraction(ctx ddd.Context, interaction cinteractiond.InteractionDesc) (id string, err error) {
	now := time.Now()
	c := collInteraction{
		InteractionID: r.NextID(),
		CreTime:       now,
		Creator:       ctx.UID(),
		Status:        0,
	}
	c.FullDesc(interaction)
	return c.InteractionID, r.DBInsert(ctx, c)
}

// 获取指定互动：根据唯一标识获取互动实体
func (r interactionRepo) GetInteraction(ctx ddd.Context, interactionid string) (*cinteractiond.InteractionAggregate, error) {
	a, err := r.GetByCollAggregate(ctx, &collInteraction{InteractionID: interactionid})
	if err != nil || a == nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a.(*cinteractiond.InteractionAggregate), nil
}

// 分页获取互动列表：根据唯一标识获取下一页指定数量的互动实体集合
// total 总数量
func (r interactionRepo) QueryPage(ctx ddd.Context, is cinteractiond.InteractionSearch, pi common.PageInfo) (pr common.PageResult, result []cinteractiond.InteractionAggregate, err error) {

	query := bson.M{
		"status": 0,
	}
	if len(is.Codes) > 0 {
		query["code"] = bson.M{"$in": is.Codes}
	}
	if len(is.IGID) > 0 {
		query["igid"] = bson.M{"$in": is.IGID}
	}
	if len(is.UID) == 1 {
		query["uid"] = is.UID[0]
	} else if len(is.UID) > 1 {
		query["uid"] = bson.M{"$in": is.UID}
	}
	var cs []collInteraction
	pr, err = r.DBQueryPage(ctx, collNameInteraction, pi, "_id", query, nil, nil, &cs)
	if len(cs) == 0 || err != nil {
		if err == mgo.ErrNotFound {
			err = nil
		}
		return
	}
	for _, c := range cs {
		a := c.ToInteractionAggregate()
		a.Init(a, r.AggregateRoot(), a.InteractionID)
		result = append(result, *a)
	}
	return
}

// 分页获取互动列表：根据唯一标识获取下一页指定数量的互动实体集合
// total 总数量
func (r interactionRepo) QueryNum(ctx ddd.Context, is cinteractiond.InteractionSearch) (num int, err error) {

	query := bson.M{
		"status": 0,
	}
	if len(is.Codes) > 0 {
		query["code"] = bson.M{"$in": is.Codes}
	}
	if len(is.IGID) > 0 {
		query["igid"] = bson.M{"$in": is.IGID}
	}
	if len(is.UID) == 1 {
		query["uid"] = is.UID[0]
	} else if len(is.UID) > 1 {
		query["uid"] = bson.M{"$in": is.UID}
	}
	num, err = r.DBQueryNum(ctx, collNameInteraction, query)
	if err != nil {
		if err == mgo.ErrNotFound {
			return 0, nil
		}
		err = errors.Wrapf(err, "查询匹配的互动信息数量失败：%v", query)
	}
	return
}

// 修改互动值
func (r interactionRepo) UpdateValue(ctx ddd.Context, interactionid string, value int) error {
	update := bson.M{"value": value}
	return r.DBUpdateCollAggregate(ctx, &collInteraction{InteractionID: interactionid}, update)
}

// 删除互动 标记删除
func (r interactionRepo) Delete(ctx ddd.Context, interactionid string) error {
	return r.DestroyByCollAggregate(ctx, &collInteraction{InteractionID: interactionid})
}

// 彻底删除指定互动
func (r interactionRepo) Destroy(ctx ddd.Context, interactionid string) error {
	return r.DestroyByCollAggregate(ctx, &collInteraction{InteractionID: interactionid})
}
